
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>模块化 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="class和继承.html" />
    
    
    <link rel="prev" href="Promise.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="变量.html">
            
                <a href="变量.html">
            
                    
                    变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="解构.html">
            
                <a href="解构.html">
            
                    
                    解构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="字符串.html">
            
                <a href="字符串.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="数组.html">
            
                <a href="数组.html">
            
                    
                    数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="对象.html">
            
                <a href="对象.html">
            
                    
                    对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="prototype.html">
            
                <a href="prototype.html">
            
                    
                    prototype
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="Promise.html">
            
                <a href="Promise.html">
            
                    
                    Promise
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.10" data-path="模块化.html">
            
                <a href="模块化.html">
            
                    
                    模块化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="class和继承.html">
            
                <a href="class和继承.html">
            
                    
                    class和继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="this.html">
            
                <a href="this.html">
            
                    
                    this
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="Symbol和generator.html">
            
                <a href="Symbol和generator.html">
            
                    
                    Symbol和generator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="async_await.html">
            
                <a href="async_await.html">
            
                    
                    async_await
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="Set和WeakSet.html">
            
                <a href="Set和WeakSet.html">
            
                    
                    Set和WeakSet
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="Map和WeakMap.html">
            
                <a href="Map和WeakMap.html">
            
                    
                    Map和WeakMap
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="数字和Math.html">
            
                <a href="数字和Math.html">
            
                    
                    数字和Math
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="正则.html">
            
                <a href="正则.html">
            
                    
                    正则
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="Proxy.html">
            
                <a href="Proxy.html">
            
                    
                    Proxy
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >模块化</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x6A21;&#x5757;&#x5316;">&#x6A21;&#x5757;&#x5316;</h1>
<p>&#x5728;ES6&#x4E4B;&#x524D;, &#x793E;&#x533A;&#x5236;&#x5B9A;&#x4E86;&#x4E00;&#x5957;&#x6A21;&#x5757;&#x89C4;&#x8303;:</p>
<pre><code>Commonjs       &#x4E3B;&#x8981;&#x670D;&#x52A1;&#x5668; nodeJs require(&apos;http&apos;)
AMD            requireJs, curlJs
CMD            seaJs
</code></pre><p>ES6&#x7EDF;&#x4E00;&#x4E86;&#x6A21;&#x5757;&#x89C4;&#x8303;</p>
<p>&#x4F7F;&#x7528;&#x6A21;&#x5757;&#x5316;&#x8981;&#x70B9;:</p>
<ol>
<li>&#x4F7F;&#x7528;&#x6A21;&#x5757;&#x5316;&#x9700;&#x8981;&#x670D;&#x52A1;&#x5668;&#x8FD0;&#x884C;&#x73AF;&#x5883;  </li>
<li>&#x5982;&#x4F55;&#x5B9A;&#x4E49;&#x6A21;&#x5757;(&#x4E00;&#x4E2A;&#x6A21;&#x5757;&#x5C31;&#x662F;&#x4E00;&#x4E2A;js&#x6587;&#x4EF6;)<pre><code class="lang-js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> a = <span class="hljs-number">12</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">let</span> b = <span class="hljs-number">13</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> c = <span class="hljs-string">&quot;hello&quot;</span>;
</code></pre>
</li>
<li>&#x5982;&#x4F55;&#x4F7F;&#x7528;(&#x5F15;&#x5165;)<pre><code class="lang-js">&lt;script type=&quot;module&quot;&gt;
  // import &apos;./modules/1.js&apos; // &#x8FD9;&#x6837;&#x76F8;&#x5F53;&#x4E8E;&#x5F15;&#x7528;&#x5916;&#x90E8;js&#x6587;&#x4EF6;(src)
  import {a, b, c} from &apos;./modules/1.js&apos;;
  // import * as mod from &apos;./modules/1.js&apos; // &#x5F15;&#x5168;&#x90E8;, &#x4F7F;&#x7528;: console.log(mod.a, mod.b, mod.b);
&lt;/script&gt;
</code></pre>
</li>
</ol>
<h3 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;:</h3>
<p><strong>&#x5B9A;&#x4E49;&#x6A21;&#x5757;</strong> </p>
<pre><code class="lang-js"><span class="hljs-comment">// modules/1.js</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;modules/1.js&#x52A0;&#x8F7D;&#x4E86;&quot;</span>);
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> a = <span class="hljs-number">12</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">let</span> b = <span class="hljs-number">13</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> c = <span class="hljs-string">&quot;hello&quot;</span>;
</code></pre>
<p>&#x6CE8;: &#x5B9A;&#x4E49;&#x6A21;&#x5757;&#x4E2D;&#x7684;&#x5185;&#x5BB9;, &#x4E5F;&#x53EF;&#x4EE5;&#x7528;&#x4E0B;&#x9762;&#x8FD9;&#x4E00;&#x79CD;&#x5F62;&#x5F0F;:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> a = <span class="hljs-number">12</span>;
<span class="hljs-keyword">let</span> b = <span class="hljs-number">13</span>;
<span class="hljs-keyword">const</span> c = <span class="hljs-string">&quot;hello&quot;</span>;

<span class="hljs-keyword">export</span> {
     a, <span class="hljs-comment">// a as A (&#x53EF;&#x4EE5;&#x8D77;&#x522B;&#x540D;)</span>
     b, 
     c
}
</code></pre>
<p><strong>&#x4F7F;&#x7528;&#x6A21;&#x5757;</strong></p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>test<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;module&quot;</span>&gt;</span><span class="javascript">
        <span class="hljs-comment">//import &apos;./modules/1.js&apos; //&#x76F8;&#x5BF9;&#x8DEF;&#x5F84;</span>
        <span class="hljs-comment">//import&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x76F8;&#x5BF9;&#x8DEF;&#x5F84;,&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;: import &apos;https://code.jquery.com/jquery-3.6.0.min.js&apos;</span>
        <span class="hljs-comment">//import&#x6A21;&#x5757;&#x53EA;&#x4F1A;&#x5BFC;&#x5165;&#x4E00;&#x6B21;, &#x8C03;&#x7528;&#x591A;&#x6B21;import&#x4E5F;&#x53EA;&#x4F1A;&#x5BFC;&#x5165;&#x4E00;&#x6B21;</span>
        <span class="hljs-comment">//import &apos;xxx.js&apos; &#x5982;&#x679C;&#x8FD9;&#x6837;&#x7528;, &#x76F8;&#x5F53;&#x4E8E;&#x5F15;&#x5165;&#x6587;&#x4EF6;</span>
        <span class="hljs-comment">/**
        setTimeout(()=&gt;{
            $(function() {
                $(&apos;body&apos;).css({
                    background: &apos;red&apos;
                })
            })
        }, 3000);
         */</span>

        <span class="hljs-comment">//import {a} from &apos;./modules/1.js&apos;;</span>
        <span class="hljs-comment">//console.log(a);</span>
        <span class="hljs-keyword">import</span> {a, b, c} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./modules/1.js&apos;</span>;
        <span class="hljs-built_in">console</span>.log(a, b, c);

    <span class="hljs-comment">// &#x53EF;&#x4EE5;&#x8D77;&#x522B;&#x540D;</span>
    <span class="hljs-keyword">import</span> {a <span class="hljs-keyword">as</span> A, b <span class="hljs-keyword">as</span> B, c <span class="hljs-keyword">as</span> C} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./modules/1.js&apos;</span>;
        <span class="hljs-built_in">console</span>.log(A, B, C);
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<h3 id="default-&#x7684;-export">default &#x7684; export</h3>
<p>&#x4E00;&#x4E2A;js&#x6587;&#x4EF6;&#x53EA;&#x80FD;&#x6709;&#x4E00;&#x4E2A;default&#x7684;export, &#x4F7F;&#x7528;&#x4E86;default&#x4E4B;&#x540E;&#x7684;export, &#x5916;&#x90E8;&#x5728;&#x5F15;&#x7528;&#x7684;&#x65F6;&#x5019;&#x4E0D;&#x9700;&#x8981;&#x52A0;&#x5927;&#x62EC;&#x53F7;, &#x793A;&#x4F8B;:</p>
<p><strong>&#x5B9A;&#x4E49;&#x6A21;&#x5757;</strong></p>
<pre><code class="lang-js"><span class="hljs-comment">// module/1.js</span>
<span class="hljs-keyword">const</span> a = <span class="hljs-number">12</span>;
<span class="hljs-keyword">let</span> b = <span class="hljs-number">13</span>;
<span class="hljs-keyword">const</span> c = <span class="hljs-string">&quot;hello&quot;</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
    a,
    b,
    c
}
</code></pre>
<p><strong>&#x4F7F;&#x7528;&#x6A21;&#x5757;</strong></p>
<pre><code class="lang-html"><span class="hljs-comment">&lt;!--use-module.html--&gt;</span>
<span class="hljs-meta">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>test<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;module&quot;</span>&gt;</span><span class="javascript">
        <span class="hljs-keyword">import</span> mod <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./modules/1.js&apos;</span>;
        <span class="hljs-built_in">console</span>.log(mod.a, mod.b, mod.c);
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<h3 id="&#x518D;&#x6B21;&#x793A;&#x4F8B;">&#x518D;&#x6B21;&#x793A;&#x4F8B;</h3>
<pre><code class="lang-html"><span class="hljs-comment">&lt;!--use-module.html--&gt;</span>
<span class="hljs-meta">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>test<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;module&quot;</span>&gt;</span><span class="javascript">
        <span class="hljs-keyword">import</span> mod, {show, sum, a, b} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./modules/1.js&apos;</span>
        <span class="hljs-keyword">let</span> p1 = <span class="hljs-keyword">new</span> mod.Person(<span class="hljs-string">&apos;daliu&apos;</span>, <span class="hljs-number">18</span>);
        p1.showName(); <span class="hljs-comment">// my name is  daliu</span>
        show(); <span class="hljs-comment">// a + b:  17</span>
        <span class="hljs-built_in">console</span>.log(sum(<span class="hljs-number">3</span>, <span class="hljs-number">5</span>)); <span class="hljs-comment">// 8</span>
        <span class="hljs-built_in">console</span>.log(a, b); <span class="hljs-comment">// 5 12</span>
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<pre><code class="lang-js"><span class="hljs-comment">// modules/1.js</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;modules/1.js&#x52A0;&#x8F7D;&#x4E86;&quot;</span>);
<span class="hljs-keyword">const</span> a = <span class="hljs-number">5</span>;
<span class="hljs-keyword">let</span> b = <span class="hljs-number">12</span>;

<span class="hljs-keyword">const</span> sum = (a, b) =&gt; {
    <span class="hljs-keyword">return</span> a + b;
}

<span class="hljs-keyword">const</span> show = () =&gt; {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;a + b: &quot;</span>, a + b);
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> </span>{
    <span class="hljs-keyword">constructor</span>(name, age) {
        <span class="hljs-keyword">this</span>.name = name;
        <span class="hljs-keyword">this</span>.age = age;
    }
    showName() {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;my name is &quot;</span>, <span class="hljs-keyword">this</span>.name);
    }
    showAge() {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;my age is &quot;</span>, <span class="hljs-keyword">this</span>.age);
    }
}

<span class="hljs-keyword">export</span> {
    a,
    b,
    sum,
    show
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
    Person
}
</code></pre>
<h3 id="&#x5728;&#x7EC4;&#x4EF6;&#x4E2D;&#x5F15;&#x5165;&#x5176;&#x4ED6;&#x7EC4;&#x4EF6;">&#x5728;&#x7EC4;&#x4EF6;&#x4E2D;&#x5F15;&#x5165;&#x5176;&#x4ED6;&#x7EC4;&#x4EF6;</h3>
<pre><code class="lang-js"><span class="hljs-comment">// modules/1.js</span>
<span class="hljs-keyword">const</span> a = <span class="hljs-number">5</span>;
<span class="hljs-keyword">let</span> b = <span class="hljs-number">12</span>;

<span class="hljs-keyword">export</span> {
    a,
    b
}
</code></pre>
<pre><code class="lang-js"><span class="hljs-comment">// modules/2.js</span>
<span class="hljs-keyword">import</span> {a, b} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./1.js&apos;</span>
</code></pre>
<h3 id="import-&#x7684;&#x63D0;&#x5347;&#x6548;&#x679C;">import &#x7684;&#x63D0;&#x5347;&#x6548;&#x679C;</h3>
<pre><code class="lang-js">&lt;script type=&quot;module&quot;&gt;
    console.log(a, b); // &#x6B64;&#x53E5;&#x4EE3;&#x7801;&#x867D;&#x7136;&#x5728;import&#x4E4B;&#x524D;, &#x4F46;&#x4F9D;&#x7136;&#x6709;&#x6548;
    import {a, b} from &apos;./modules/1.js&apos; // &#x8FD9;&#x53E5;&#x4EE3;&#x7801;&#x4F1A;&#x88AB;&#x63D0;&#x5347;&#x5230;&#x9876;&#x90E8;
&lt;/script&gt;
</code></pre>
<h3 id="&#x6A21;&#x5757;&#x5185;&#x7684;&#x5B9A;&#x65F6;&#x5668;&#x66F4;&#x6539;&#x6570;&#x636E;&#x5F71;&#x54CD;&#x5916;&#x90E8;">&#x6A21;&#x5757;&#x5185;&#x7684;&#x5B9A;&#x65F6;&#x5668;&#x66F4;&#x6539;&#x6570;&#x636E;&#x5F71;&#x54CD;&#x5916;&#x90E8;</h3>
<pre><code class="lang-js"><span class="hljs-comment">// modules/1.js</span>
<span class="hljs-keyword">let</span> a = <span class="hljs-number">5</span>;
<span class="hljs-keyword">let</span> b = <span class="hljs-number">12</span>;

setTimeout(() =&gt; {
    a = <span class="hljs-number">111</span>;
    b = <span class="hljs-number">222</span>;
}, <span class="hljs-number">2000</span>); <span class="hljs-comment">// &#x8FD9;&#x91CC;&#x7684;&#x66F4;&#x6539;&#x4F1A;&#x5BFC;&#x81F4;&#x5916;&#x9762;&#x5F15;&#x7528;&#x7684;&#x5730;&#x65B9;&#x4E5F;&#x76F8;&#x5E94;&#x66F4;&#x6539;</span>

<span class="hljs-keyword">export</span> {
    a,
    b
}
</code></pre>
<pre><code class="lang-html"><span class="hljs-comment">&lt;!--use-module.html--&gt;</span>
<span class="hljs-meta">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>test<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;module&quot;</span>&gt;</span><span class="javascript">
        <span class="hljs-keyword">import</span> {a, b} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./modules/3.js&apos;</span>

        <span class="hljs-built_in">console</span>.log(a, b); <span class="hljs-comment">// 5 12</span>
        setTimeout(()=&gt;{
            <span class="hljs-built_in">console</span>.log(a, b); <span class="hljs-comment">// 111 222</span>
            <span class="hljs-comment">// &#x8FD9;&#x8868;&#x660E; import &#x7684;&#x6A21;&#x5757;&#x5185;&#x5BB9;,&#x5982;&#x679C;&#x91CC;&#x9762;&#x6709;&#x5B9A;&#x65F6;&#x5668;&#x66F4;&#x6539;,&#x5916;&#x9762;&#x4E5F;&#x4F1A;&#x6539;&#x52A8;</span>
        }, <span class="hljs-number">3000</span>);
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<h3 id="&#x52A8;&#x6001;&#x5F15;&#x5165;">&#x52A8;&#x6001;&#x5F15;&#x5165;</h3>
<p>&#x7528;&#x4E0B;&#x9762;&#x7684;&#x5F62;&#x5F0F;&#x6839;&#x636E;&#x6761;&#x4EF6;import&#x4E0D;&#x540C;&#x7684;&#x6A21;&#x5757;&#x662F;&#x4E0D;&#x5141;&#x8BB8;&#x7684;:</p>
<pre><code class="lang-js">&lt;script type=&quot;module&quot;&gt;
let a = 12;
if (a == 12) {
    import {a} from &apos;./modules/1.js&apos;;
} else {
    import {b} from &apos;./modules/2.js&apos;;
}
&lt;/script&gt;
</code></pre>
<p>&#x52A0;&#x8F7D;&#x52A8;&#x6001;&#x6A21;&#x5757;, &#x8981;&#x4F7F;&#x7528;import()&#x7684;&#x65B9;&#x5F0F;:</p>
<pre><code class="lang-js">&lt;script type=&quot;module&quot;&gt;
    let a = 12;
    if (a == 12) {
        import(&apos;./modules/3.js&apos;).then(res=&gt;{
            console.log(res.a, res.b);
        }).catch(()=&gt;{

        });
    } else {
        // ...
    }
&lt;/script&gt;
</code></pre>
<p>&#x8DEF;&#x5F84;&#x53EF;&#x4EE5;&#x662F;&#x7EDD;&#x5BF9;&#x7684;&#x6216;&#x52A8;&#x6001;&#x7684;:</p>
<pre><code class="lang-js">&lt;!DOCTYPE html&gt;
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>test<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;module&quot;</span>&gt;</span><span class="javascript">
        <span class="hljs-keyword">let</span> a = <span class="hljs-number">12</span>;
        <span class="hljs-keyword">if</span> (a == <span class="hljs-number">12</span>) {
            <span class="hljs-keyword">import</span>(<span class="hljs-string">&apos;https://code.jquery.com/jquery-3.6.0.min.js&apos;</span>).then(res=&gt;{
                $(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
                    $(<span class="hljs-string">&apos;body&apos;</span>).css({
                        background: <span class="hljs-string">&apos;green&apos;</span>
                    });
                });
            });
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-comment">// ...</span>
        }
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span></span>
</code></pre>
<p>import() &#x8FD4;&#x56DE;&#x7684;&#x662F;&#x4E00;&#x4E2A;Promise, &#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;Promise.all()&#x52A0;&#x8F7D;&#x591A;&#x4E2A;&#x6A21;&#x5757;:</p>
<pre><code class="lang-js"><span class="hljs-comment">// modules/1.js</span>
<span class="hljs-keyword">const</span> A = <span class="hljs-number">5</span>;

<span class="hljs-keyword">export</span> {
    A
}
</code></pre>
<pre><code class="lang-js"><span class="hljs-comment">// modules/2.js</span>
<span class="hljs-keyword">let</span> B = <span class="hljs-string">&apos;B&apos;</span>

<span class="hljs-keyword">export</span> {
    B
}
</code></pre>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>test<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;module&quot;</span>&gt;</span><span class="javascript">
        <span class="hljs-built_in">Promise</span>.all([
            <span class="hljs-keyword">import</span>(<span class="hljs-string">&apos;./modules/1.js&apos;</span>),
            <span class="hljs-keyword">import</span>(<span class="hljs-string">&apos;./modules/2.js&apos;</span>),
        ]).then(([mod1, mod2])=&gt;{
            <span class="hljs-built_in">console</span>.log(mod1.A, mod2.B);
        })
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>&#x52A8;&#x6001;&#x5F15;&#x5165;&#x518D;&#x793A;&#x4F8B;:  </p>
<pre><code class="lang-js">&lt;script type=<span class="hljs-string">&quot;module&quot;</span>&gt;
    <span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">test</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-comment">//const mod1 = await import(&apos;./modules/1.js&apos;);</span>
        <span class="hljs-comment">//const mod2 = await import(&apos;./modules/2.js&apos;);</span>
        <span class="hljs-keyword">const</span> [m1, m2] = <span class="hljs-keyword">await</span> <span class="hljs-built_in">Promise</span>.all([
            <span class="hljs-keyword">import</span>(<span class="hljs-string">&apos;./modules/1.js&apos;</span>),
            <span class="hljs-keyword">import</span>(<span class="hljs-string">&apos;./modules/2.js&apos;</span>)
        ]);
        <span class="hljs-built_in">console</span>.log(m1.A, m2.B);
    }

    test();
<span class="xml"><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></span>
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="Promise.html" class="navigation navigation-prev " aria-label="Previous page: Promise">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="class和继承.html" class="navigation navigation-next " aria-label="Next page: class和继承">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"模块化","level":"1.10","depth":1,"next":{"title":"class和继承","level":"1.11","depth":1,"path":"class和继承.md","ref":"class和继承.md","articles":[]},"previous":{"title":"Promise","level":"1.9","depth":1,"path":"Promise.md","ref":"Promise.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"模块化.md","mtime":"2021-12-12T09:30:53.931Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-02-17T15:54:39.156Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

